package cloud.quan.service.impl;

import cloud.quan.service.PddService;
import com.pdd.pop.sdk.common.util.JsonUtil;
import com.pdd.pop.sdk.http.PopClient;
import com.pdd.pop.sdk.http.api.pop.request.*;
import com.pdd.pop.sdk.http.api.pop.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PddServiceImpl implements PddService {

    @Autowired
    PopClient pddClient;

    @Override
    public String goodsDetail(Long goodsId, String pId) throws Exception {
        PddDdkGoodsDetailRequest request = new PddDdkGoodsDetailRequest();
        List<Long> goodsIdList = new ArrayList();
        goodsIdList.add(goodsId);
        request.setGoodsIdList(goodsIdList);
        request.setPid(pId);
        PddDdkGoodsDetailResponse response = null;
        response = pddClient.syncInvoke(request);
        return JsonUtil.transferToJson(response);
    }

    @Override
    public String goodsSearch(String keyword, Integer page, Integer pageSize, Integer sortType, String pId, String listId) throws Exception {
        PddDdkGoodsSearchRequest request = new PddDdkGoodsSearchRequest();

        request.setKeyword(keyword);
        request.setPage(page);
        request.setPageSize(pageSize);
        request.setPid(pId);
        request.setSortType(sortType);
        request.setListId(listId);

        PddDdkGoodsSearchResponse response = null;
        response = pddClient.syncInvoke(request);
        return JsonUtil.transferToJson(response);
    }

    @Override
    public String goodsOptGet(Integer parentOptId) throws Exception {
        PddGoodsOptGetRequest request = new PddGoodsOptGetRequest();
        request.setParentOptId(parentOptId);
        PddGoodsOptGetResponse response = null;
        response = pddClient.syncInvoke(request);
        return JsonUtil.transferToJson(response);
    }

    @Override
    public String goodsCatsGet(Long parentCatId) throws Exception {
        PddGoodsCatsGetRequest request = new PddGoodsCatsGetRequest();
        request.setParentCatId(parentCatId);
        PddGoodsCatsGetResponse response = pddClient.syncInvoke(request);
        return JsonUtil.transferToJson(response);
    }

    @Override
    public String themeListGet(Integer pageSize, Integer page) throws Exception {
        PddDdkThemeListGetRequest request = new PddDdkThemeListGetRequest();
        request.setPageSize(pageSize);
        request.setPage(page);
        PddDdkThemeListGetResponse response = null;
        response = pddClient.syncInvoke(request);
        return JsonUtil.transferToJson(response);
    }

    @Override
    public String themeGoodsSearch(Long themeId) throws Exception {
        PddDdkThemeGoodsSearchRequest request = new PddDdkThemeGoodsSearchRequest();
        request.setThemeId(themeId);
        PddDdkThemeGoodsSearchResponse response = null;
        response = pddClient.syncInvoke(request);
        return JsonUtil.transferToJson(response);
    }


    @Override
    public String goodsPromotionUrlGenerate(String pId, Long goodsId, String searchId) throws Exception {
        PddDdkGoodsPromotionUrlGenerateRequest request = new PddDdkGoodsPromotionUrlGenerateRequest();
        request.setPId(pId);
        // 是否生成微博推广链接
        request.setGenerateWeappWebview(true);
        // 是否生成小程序推广
        request.setGenerateWeApp(true);
        // 搜索id，建议填写，提高收益
        // 来自pdd.ddk.goods.recommend.get、pdd.ddk.goods.search、pdd.ddk.top.goods.list.query等接口
        request.setSearchId(searchId);
        List<Long> goodsIdList = new ArrayList();
        goodsIdList.add(goodsId);
        request.setGoodsIdList(goodsIdList);

        PddDdkGoodsPromotionUrlGenerateResponse response = null;
        response = pddClient.syncInvoke(request);
        return JsonUtil.transferToJson(response);
    }
}
